#!/usr/bin/env python3
"""
检查DataPlexus后端所有Python文件的语法错误
"""

import ast
import sys
from pathlib import Path

def check_syntax(file_path):
    """检查单个文件的语法"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # 尝试解析AST
        ast.parse(content)
        return True, None
        
    except SyntaxError as e:
        return False, f"语法错误: 行 {e.lineno}, 列 {e.offset}: {e.msg}"
    except Exception as e:
        return False, f"其他错误: {e}"

def main():
    print("🔍 检查DataPlexus数据治理平台后端Python文件语法")
    print("=" * 50)
    
    backend_dir = Path(__file__).parent.parent / 'backend'
    python_files = list(backend_dir.rglob('*.py'))
    
    error_files = []
    
    for file_path in python_files:
        relative_path = file_path.relative_to(backend_dir)
        is_valid, error_msg = check_syntax(file_path)
        
        if not is_valid:
            print(f"❌ {relative_path}")
            print(f"   {error_msg}")
            error_files.append(str(relative_path))
        else:
            print(f"✅ {relative_path}")
    
    print()
    print(f"📊 检查结果:")
    print(f"   - 总文件数: {len(python_files)}")
    print(f"   - 语法正确: {len(python_files) - len(error_files)}")
    print(f"   - 语法错误: {len(error_files)}")
    
    if error_files:
        print(f"\n❌ 需要修复的文件:")
        for file in error_files:
            print(f"   📁 {file}")
        return False
    else:
        print(f"\n🎉 所有文件语法检查通过！")
        return True

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)
